﻿@model Apps.Models.HuaChan.HUACHAN_PROJECT
@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models.PUB;
@using Apps.Web;
@using Apps.Locale;
@using Apps.Models.Sys;
@using Apps.Models.Enum;
@{
    ViewBag.Title = "创建";
    Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
    List<permModel> perm = null;
}

<script type="text/javascript">
    $(function () {
        function GeChangedData() {
            var model = {
                id: $("#ID").val(),
                name: $("#NAME").val(),
                isuse: $("#ISUSE").val(),
                sort: $("#SORT").val(),
                fields: $("#List").datagrid("getRows")
            };
            return model;
        }
        $("#btnSave").click(function () {
            if (editIndex != undefined) {
                return window.parent.frameReturnByMes("存在未确认的参考指标数据，请先确认");
            }
            if ($("form").valid()) {
                $.ajax({
                    url: "@Url.Action("Create")",
                    type: "Post",
                    data: GeChangedData(),
                    dataType: "json",
                    success: function (data) {
                        if (data.type == 1) {
                            window.parent.frameReturnByMes(data.message);
                            window.parent.frameReturnByReload(true);
                            window.parent.frameReturnByClose()
                        }
                        else {
                            window.parent.frameReturnByMes(data.message);
                        }
                    }
                });
            }
            return false;
        });
        $("#btnReturn").click(function () {
            window.parent.frameReturnByClose();
        });
    });
</script>
<div class="mvctool bgb">
    @Html.ToolButton("btnSave", "fa fa-save", Resource.Save, ref perm, "Save", true)
    @Html.ToolButton("btnReturn", "fa fa-reply", Resource.Reply, false)
</div>
@using (Html.BeginForm())
{
    @Html.Hidden("ID", ResultHelper.NewId)
    <table class="formtable">
        <tbody>
            <tr>
                <th style="width:80px">
                    <label for="NAME"><font color="red">*</font>标题显示</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="NAME" name="NAME" type="text" value="" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="ISUSE"><font color="red">*</font>启用状态</label>：
                </th>
                <td>
                    <select id="ISUSE" name="ISUSE" autocomplete="off"><option selected="selected" value="1">启用</option><option value="0">禁用</option></select>
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="SORT"><font color="red">*</font>排序</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="SORT" name="SORT" type="text" value="1" autocomplete="off">
                </td>
                <th style="width:80px">
                </th>
                <td>
                </td>
            </tr>
            <tr>
                <td colspan="5" style="vertical-align: top; ">
                    <table id="List"></table>
                </td>
            </tr>
        </tbody>
    </table>
    @Html.ValidationSummary()
}
<script type="text/javascript">
    var editIndex = undefined;
    var changeRow;
    var effectRow = new Object();
    function endEditing() {
        if (editIndex == undefined) { return true }
        if ($('#List').datagrid('validateRow', editIndex)) {
            $('#List').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#List').datagrid('selectRow', index).datagrid('beginEdit', index);
                editIndex = index;
            } else {
                $('#List').datagrid('selectRow', editIndex);
            }
        }
    }

    function onClickCell(index, field) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#List').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                var ed = $('#List').datagrid('getEditor', { index: index, field: field });
                if (ed) {
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                }
                editIndex = index;
            } else {
                setTimeout(function () {
                    $('#List').datagrid('selectRow', editIndex);
                }, 0);
            }
        }
    }
    function onEndEdit(index, row) {
        var ed = $(this).datagrid('getEditor', {
            index: index,
            field: 'ID'
        });
        //row.productname = $(ed.target).combobox('getText');
    }
    function append() {
        if (endEditing()) {
            $('#List').datagrid('appendRow', { ID: '@ResultHelper.NewId', BASEVALUE: '', FIELDTYPE:'文本', MINVALUE: 0.000, MAXVALUE:0.000 });
            editIndex = $('#List').datagrid('getRows').length - 1;
            $('#List').datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
        }
    }
    function delrow() {
         
        if (editIndex < 0) { $.messager.alert('Warning', '请选择要删除的行!'); return; }
        if (editIndex == undefined) { $.messager.alert('Warning', '请选择要删除的行!'); return; }
        $.messager.confirm(index_lang_tip, '确定删除此行吗?', function (r) {
            if (r) {
                $('#List').datagrid('deleteRow', editIndex);
                     editIndex = undefined;
                }
        });

    }
    function acceptit() {
        if (endEditing()) {
            var changed = $('#List').datagrid('getChanges');
            if (changed.length > 0) {
                changeRow = new Array();
                for (var i = 0; i < changed.length; i++) {
                    if (dataCheck(changed[i].MAXVALUE) && dataCheck(changed[i].MINVALUE)) { changeRow.push(changed[i]); }
                }
            }
            editIndex = undefined;
        }
    }
    function reject() {
        $('#List').datagrid('rejectChanges');
        editIndex = undefined;
    }
    function dataCheck(values) {
        if (values != '0.000' && values != '0' && values != '' && values != undefined) {
            return true;
        }
        return false;
    }
    function getChanges() {
        var rows = $('#List').datagrid('getChanges');
        alert(rows.length + ' rows are changed!');
    }
    function fmCheck(value, row, index) {
        if (value == '是' || value == '1') {
            return "是";
        } else {
            return "否";
        }
    }
    $(function () {
        var id = '';
        $('#List').datagrid({
            url: '@Url.Action("GetListDetail")?projectId=' + id,
            width: SetGridWidthSub(30),
            methord: 'post',
            height: 550,
            fitColumns: true,
            sortName: 'ID',
            sortOrder: 'desc',
            idField: 'ID',
            striped: true, //奇偶行是否区分
            singleSelect: true,
            rownumbers: true,//行号
            onClickRow: onClickRow,
            onEndEdit: onEndEdit,
            onLoadSuccess: function(data) {
            },
            columns: [[
                { field: 'ID', title: 'ID', width: 20, hidden: true },
                {
                    field: 'FIELDNAME', title: '字段名称', width: 60,
                    editor: {
                        type: 'textbox'
                    }
                },

                {
                    field: 'FIELDTYPE', title: '字段类型', width: 100, sortable: true,
                    editor: {
                        type: 'combobox',
                        options: {
                            data: [{  Name: "文本" }, {  Name: "数值" }, {  Name: "时间" }],
                            valueField: 'Name',
                            textField: 'Name',
                            panelHeight: 'auto',
                            editable: false
                        }
                    }
                },
                {
                    field: 'REQUIRED', title: '是否必填', width: 50, sortable: true,
                    formatter: fmCheck,
                    editor: {
                        type: 'checkbox', options: { on: "是", off:"否"}
                    }
                },
                {
                    field: 'BASEVAL', title: '基值', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 4
                        }
                    }
                },
                {
                    field: 'MIN', title: '最小值', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 4
                        }
                    }
                },
                {
                    field: 'MAX', title: '最大值', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 4
                        }
                    }
                },
                {
                    field: 'KEEPPOINT', title: '小数点位', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 0
                        }
                    }
                },
                {
                    field: 'SORT', title: '排序', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 0
                        }
                    }
                }
            ]],
            toolbar: [
                {
                    text: '添加',
                    handler: function () {
                        append();
                    }
                },
                '-',
                {
                    text: '取消',
                    handler: function () {
                        reject();
                    }
                },
                '-',
                {
                    text: '删除',
                    handler: function () {
                        delrow();
                    }
                }, '-',
                {
                    text: "确认",
                    handler: function () {
                        acceptit();
                    }
                }],
        });
    });
</script>